/**
 * $Id: LoginSuccessHandler.java 12 2010-11-02 15:33:09Z feigme $
 */
package com.iary.security;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

public class LoginSuccessHandler implements AuthenticationSuccessHandler {

	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
			Authentication authentication) throws IOException, ServletException {
		response.setContentType("application/json;charset=UTF-8");
		Collection<GrantedAuthority> c = authentication.getAuthorities();
		StringBuffer str = new StringBuffer("{");
		str.append("\"email\":\"" + authentication.getName() + "\"");
		for (GrantedAuthority ga : c) {
			if (ga.getAuthority().equals("ROLE_ADMIN")) {
				str.append(",\"role\":\"" + ga.getAuthority() + "\"");
			}
		}
		str.append("}");
		response.getWriter().write(str.toString());
	}

}
